using System.Collections.Generic;
using System.Linq;
using SiemensAmd.Mmt.McisDbModel;
using SqlSugar;
using SqlSugarEx.Utilities;

namespace SiemensAmd.Mmt.McisDbDal
{
	public class TdiToolPlaceDal
	{
		private readonly SqlSugarClient _sqlSugar;

		public TdiToolPlaceDal(string server, string db, string user, string pwd)
		{
			this._sqlSugar = new SqlSugarClient(new ConnectionConfig
			{
				DbType = DbType.SqlServer,
				ConnectionString = DbUtility.SQLDbFormat(server, db, user, pwd),
				IsAutoCloseConnection = false
			});
		}

		public List<ToolPlaceEntity> QueryAllPlace(bool? isEmpty = null, params string[] containnerId)
		{
			List<ToolPlaceEntity> list = null;
			ISugarQueryable<ToolPlaceEntity> sugarQueryable = this._sqlSugar.Queryable<ToolPlaceEntity>().WhereIF(containnerId != null, (ToolPlaceEntity x) => SqlFunc.ContainsArray(containnerId, x.ContainerId));
			if (!isEmpty.HasValue)
			{
				return sugarQueryable.ToList();
			}
			if (isEmpty == true)
			{
				return sugarQueryable.Where((ToolPlaceEntity x) => x.DbIdTDIToolData == 0).ToList();
			}
			return sugarQueryable.Where((ToolPlaceEntity x) => x.DbIdTDIToolData > 0).ToList();
		}

		public TDI_TOOLPLACEEntity QueryTdiToolPlace(long idTdiToolData)
		{
			return (from x in this._sqlSugar.Queryable<TDI_TOOLPLACEEntity>()
				where x.DbIdTDIToolData == idTdiToolData
				select x).ToList().FirstOrDefault();
		}
	}
}
